// preferences.js

var DEFAULTS_PREF_KEY = "extensions.appalachian.useDefault";
var DEFAULTS_EL_ID = "preferences-defaults-checkbox";
var SHOW_STATUS_PREF_KEY = "extensions.appalachian.showInStatusBar";
var SHOW_STATUS_EL_ID = "preferences-show-status-checkbox";

var AppalachianPrefs = {};

AppalachianPrefs.onLoad = function() {
	var defaults = AppalachianPrefs.getPref(DEFAULTS_PREF_KEY);
	var statusbar = AppalachianPrefs.getPref(SHOW_STATUS_PREF_KEY);

	if (defaults) {
		var d = document.getElementById(DEFAULTS_EL_ID);
		d.setAttribute("checked", true);
	}

	if (statusbar) {
		var s = document.getElementById(SHOW_STATUS_EL_ID);
		s.setAttribute("checked", true);
	}
};

AppalachianPrefs.save = function() {
	var defaults = document.getElementById(DEFAULTS_EL_ID);
	var status = document.getElementById(SHOW_STATUS_EL_ID);

	AppalachianPrefs.setPref(DEFAULTS_PREF_KEY, defaults.checked);
	AppalachianPrefs.setPref(SHOW_STATUS_PREF_KEY, status.checked);

	var par = window.opener;
	while (par.opener) { par = par.opener; }
	var icon = par.document.getElementById("appalachian-status-icon");
	if (icon) {
		if (status.checked)
			icon.style.display = 'block';
		else
			icon.style.display = 'none';
	}
};

AppalachianPrefs.getPref = function(key) {
	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	return prefs.getBoolPref(key);
};

AppalachianPrefs.setPref = function(key, value) {
	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	prefs.setBoolPref(key, value);	
};

AppalachianPrefs.getUsingDefault = function() {
	return AppalachianPrefs.getPref(DEFAULTS_PREF_KEY);
};

AppalachianPrefs.setUsingDefault = function(using) {
	AppalachianPrefs.setPref(DEFAULTS_PREF_KEY, using);
};

AppalachianPrefs.getShowInStatusBar = function() {
	return AppalachianPrefs.getPref(SHOW_STATUS_PREF_KEY);
};

AppalachianPrefs.setShowInStatusBar = function(show) {
	AppalachianPrefs.setPref(SHOW_STATUS_PREF_KEY, show);
};

AppalachianPrefs.getString = function(key) {
	var bundle = document.getElementById("preferences-bundle");
	return bundle.getString(key);
};

AppalachianPrefs.reset = function() {
	var msg = AppalachianPrefs.getString("confirmreset");
	if (confirm(msg)) {
		AppalachianDB.wipe();
		AppalachianDB.init();
	}
};
